var text = [{
  load: '玩命加载中', end: '没有更多了哦',
  title: '我的申请', button: '发布需求', endTime: '截止至',
  blank0: '暂无', blank1: '记录',
  tab1: '审核中', tab2: '已提交申请', tab3: '已结束',
}, {
  load: 'Loading', end: 'No more...',
  title: 'My application', button: 'post a task', endTime: 'Deadline: ',
  blank0: 'No ', blank1: ' Applications',
  tab1: 'Invalid', tab2: 'Submitted', tab3: 'Ended',
}]
Page({

  data: {
    defultAvatar: "/assets/logo/logo.jpg",
    blankIcon: "/assets/module/blank.png",
    showMask: true,
    P0: [],
    P1: [],
    P2: [],
  },

  cardTap(e) {
    var id = e.currentTarget.id.split('-')[0];
    var target = this.data[`P${id}`]; // 获取对应practice列表
    for (let i = 0; i < target.length; ++i) {
      if (e.currentTarget.id.split('-')[1] == target[i].idpractice) {
        let string = JSON.stringify(target[i]);
        wx.onHide(this, `../detail/detail?content=${encodeURIComponent(string)}&status=${id}`);
        break;
      } 
    }
  },

  // 前端处理返回结果
  process(rowData) {
    var data = [];
    for (let i = 0; i < rowData.length; i++) {
      data[i] = rowData[i];
      // let userInfo = { 'avatar': rowData[i].avatar, 'nickname': rowData[i].nickName },
      let date = wx.formatTime(new Date(parseInt(data[i].practiceInfo.gmtcreate) * 1e3), 'd-t').split(' '),
      _txt = data[i].practiceInfo.description;  // cut the excess words after 3 lines
      data[i].time = date[0] == wx.formatTime(new Date()) ? date[1] : date[0];
      data[i].text = _txt.length > 40 ? _txt.substring(0, 40) + '...' : data[i].text = _txt;
      data[i].endTime = wx.formatTime(new Date(parseInt(data[i].practiceInfo.gmtend) * 1e3), '/');
      // data[i].cardId = `${tab}-${i}`;
    }
    return data;
  },

  onShow() {
    wx.onShow(this, text[wx.lang].title);
    wx.post('PracticeItem.getItemList', { idUser: wx.appData.user.iduser, tab: 0 }, res => {
      let data = res.success ? this.process(res.data.data) : [];
      this.setData({ text: text[wx.lang], P0: data });
      if (!data.length) return;  // stop processing when no data exists
      let array = [];
      for (let i = 0; i < 6; ++i) array.push('card[' + i + ']');
      wx._slide_v(this, array, wx.system.height * .8, 0, () => {
        this.setData({ loading: false });
        wx.slide_v(this, array, 0, 1, 0);
      });
    }, text[wx.lang].load);
  },

  changeTabs(e) {
    console.log(e)
    // target = !index-- ? this.data.P0 : !index ? this.data.P1 : this.data.P2;
    var target = this.data[`P${e.detail.index}`];
    if (!target.length) {
      wx.post('PracticeItem.getItemList', { idUser: wx.appData.user.iduser, tab: e.detail.index }, res => {
        let data = res.success ? this.process(res.data.data) : [];
        switch (e.detail.index) {
          case 0: this.setData({ P0: data });
            break; 
          case 1: this.setData({ P1: data });
            break;
          case 2: this.setData({ P2: data });
            break;
        }
        if (!data.length) return;  // stop processing when no data exists
        let array = [];
        for (let i = 0; i < 6; ++i) array.push('card[' + i + ']');
        wx._slide_v(this, array, wx.system.height * .8, 0, () => {
          this.setData({ loading: false });
          wx.slide_v(this, array, 0, 1, 0);
        });
      }, text[wx.lang].load);
    }
  }

})